package com.helen.ui.order;

import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RatingBar;
import android.widget.TextView;

import com.coorchice.library.SuperTextView;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.helen.adapter.SingleImgAdapter;
import com.helen.entity.CommentDetailEntity;
import com.helen.global.Constants;
import com.helen.shopping.R;
import com.helen.ui.BaseActivity;
import com.helen.utils.ImageLoadUtil;
import com.helen.utils.JsonUtil;
import com.helen.utils.MyLog;
import com.helen.utils.tiputils.MToast;
import com.zhouyou.http.EasyHttp;
import com.zhouyou.http.callback.SimpleCallBack;
import com.zhouyou.http.exception.ApiException;
import com.zhouyou.http.model.HttpParams;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

/**
 * Created by yang on 2018/7/9.
 */

public class CommentProductDetailActivity extends BaseActivity {
    @BindView(R.id.img_product)
    ImageView imgProduct;
    @BindView(R.id.tv_product_name)
    TextView tvProductName;
    @BindView(R.id.tv_product_spec)
    TextView tvProductSpec;
    @BindView(R.id.tv_product_price)
    TextView tvProductPrice;
    @BindView(R.id.tv_product_count)
    TextView tvProductCount;
    @BindView(R.id.rating_bar)
    RatingBar ratingBar;
    @BindView(R.id.et_detail_content)
    EditText etDetailContent;
    @BindView(R.id.recycler)
    RecyclerView recycler;
    @BindView(R.id.tv_comment)
    SuperTextView tvComment;
    private int iid;

    @Override
    protected int attachLayoutRes() {
        return R.layout.activity_comment_product;
    }

    @Override
    protected void initInjector() {

    }

    @Override
    protected void initViews() {
        tvComment.setVisibility(View.GONE);
        getToolbarTitle().setText("评价详情");
        iid = getIntent().getIntExtra("iid", 0);
        etDetailContent.setClickable(false);
        etDetailContent.setEnabled(false);
        commentDetailApi();
    }

    @Override
    protected void updateViews() {

    }

    private void commentDetailApi() {
        mMProgressDialog.show();
        HttpParams params = new HttpParams();
        params.put("iid", iid + "");
        EasyHttp.post(Constants.API_JSONAPI_COMMENTDETAIL)
                .params(params)
                .execute(new SimpleCallBack<String>() {

                    @Override
                    public void onCompleted() {
                        super.onCompleted();
                        mMProgressDialog.dismiss();
                    }

                    @Override
                    public void onError(ApiException e) {
                        MyLog.e("yang", "评论详情失败==" + e.toString());
                        MToast.makeTextLong(CommentProductDetailActivity.this, getString(R.string.service_error)).show();
                    }

                    @Override
                    public void onSuccess(String result) {
                        MyLog.e("yang", "评论详情==" + result);
                        int code = JsonUtil.getJsonValuesInt(result, "code");
                        String msg = JsonUtil.getJsonValuesString(result, "msg");
                        if (code == 0) {
                            Gson gson = new GsonBuilder().create();
                            CommentDetailEntity commentDetailEntity = gson.fromJson(result, CommentDetailEntity.class);



                            ImageLoadUtil.displayImage(CommentProductDetailActivity.this, imgProduct, commentDetailEntity.getProductimg(), R.mipmap.app_logo, R.mipmap.app_logo);
                            tvProductName.setText(commentDetailEntity.getProductname());
                            tvProductSpec.setText(commentDetailEntity.getSpec());
                            tvProductPrice.setText(commentDetailEntity.getPrice()+"");
                            tvProductCount.setText("1");
                            ratingBar.setRating(commentDetailEntity.getLevel());
                            etDetailContent.setText(commentDetailEntity.getContent());

                            List<String> commentImgList = new ArrayList<>();
                            for (int i = 0;i<commentDetailEntity.getImgs().size();i++){
                                commentImgList.add(commentDetailEntity.getImgs().get(i).getUrl());
                            }
                            SingleImgAdapter commentImgAdapter = new SingleImgAdapter(R.layout.item_comment_img_recycler, commentImgList);
                            recycler.setLayoutManager(new GridLayoutManager(CommentProductDetailActivity.this, 4));
                            recycler.setAdapter(commentImgAdapter);

                        } else {
                            MToast.makeTextLong(CommentProductDetailActivity.this, msg).show();
                        }

                    }
                });
    }

}
